#!/usr/bin/env python
# -*- coding:utf-8 -*- 
# @Time    : 2018/12/5 22:26
# @Author  : liujiantao
# @Site    : 
# @File    : lightgbm_train01.py
# @Software: PyCharm
from tiancheng.base.base_helper import *

# test = get_test_data()[cols]
# # 主成分分析PCA
# from sklearn.decomposition import PCA
# pca = PCA(n_components=2)
# X_train_pca = pca.fit_transform(X_train)
# X_test_pca = pca.transform(X_test)
# 用法：
# 采用 smote 算法 进行 过采样
def get_lightgbm(X, Y, i, test01):
    """
    :return:
    """
    from imblearn.over_sampling import SMOTE
    sm = SMOTE(random_state=i, k_neighbors=5)
    # 数据标准化处理：
    from sklearn.preprocessing import StandardScaler

    X = StandardScaler().fit_transform(X)
    from sklearn.model_selection import train_test_split
    X_train_res, y_train_res = sm.fit_sample(X, Y)
    # 随机抽取20%的测试集
    X_train, X_test, y_train, y_test = train_test_split(X_train_res, y_train_res, test_size=0.2, random_state=i)

    import lightgbm as lgb
    clf = lgb.LGBMClassifier(
        colsample_bytree=1.0,
        learning_rate=0.03,
        num_leaves=400,  # 决策树的叶子节点
        max_depth=-1,
        min_child_samples=8,
        min_child_weight=0.001,
        min_split_gain=0.0,
        n_estimators=10000,
        n_jobs=-1,  # 等于-1的时候，表示cpu里的所有core进行工作。
        objective='binary',  # 目标为2分类,
        reg_alpha=0.1,
        reg_lambda=0.0,
        silent=True,
        subsample=0.95,
        subsample_for_bin=20000,
        subsample_freq=1)
    clf.fit(X_train, y_train, early_stopping_rounds=50, eval_metric="logloss",
            eval_set=[(X_test, y_test)])
    ytestPre = clf.predict_proba(X_test)[:, 1]
    m = tpr_weight_funtion(y_test, ytestPre)
    print(m)
    test01 = StandardScaler().fit_transform(test01)
    ytestPre = clf.predict(X_test)

    from sklearn.metrics import accuracy_score
    sub = clf.predict_proba(test01)[:, 1]
    accuracy = accuracy_score(y_test, ytestPre)
    print(accuracy)
    return m,sub

# sub_pre = [float_to_str(item[1]) for item in sub_pre]
# sub = get_sub()
# sub['Tag'] = sub_pre
# sub.to_csv(sub_base_path + 'lightgbm_train01_%s.csv' % str(m), index=False)

if __name__ == '__main__':
    thresholds = [0, 0.1, 0.2, 3, 4, 5]
    y = get_tag_train_new()[tag_hd.Tag].values  # pd.read_csv('input/tag_train_new.txt')
    sub = get_sub()
    train = pd.read_csv(train_data_path)
    test = pd.read_csv(test_data_path)
    # test = test.drop(['UID', 'Tag'], axis=1)
    train[tag_hd.Tag] = y
    cols = list(test.columns.values)
    print(cols)
    # tr_corr = train.corr()[tag_hd.Tag].reset_index()
    # tr_corr
    print(train.shape)
    print(test.shape)
    res, weights = pd.DataFrame(), []
    for i in range(5):
        cols_sample = random.sample(cols, 250)
        X_shixin = train[train[tag_hd.Tag] == 1]
        sample_size = (i + 2) * X_shixin.shape[0]
        X_normal = train[train[tag_hd.Tag] == 0].sample(sample_size, random_state=i)
        X = pd.concat([X_normal, X_shixin])
        X0 = X[cols_sample]
        label = X[tag_hd.Tag]
        test01 = test[cols_sample]
        print(test01.shape)
        m, sub = get_lightgbm(X0, label, i, test01)
        res[i] = sub
        weights.append(m)
    print(weights)
    res.columns = [i for i in range(res.shape[1])]
    blending_model(res, weights, "lgbm_")
    print(123)
